package com.houmeiyuan.huiguniang.ctrl;
import com.houmeiyuan.huiguniang.common.MessageConstVar;
import com.houmeiyuan.huiguniang.common.Result;
import com.houmeiyuan.huiguniang.common.StringUtil;
import com.houmeiyuan.huiguniang.modles.domain.WxUser;
import com.houmeiyuan.huiguniang.service.UserService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ff
 * @date 2019-07-20 17:09
 */
@RequestMapping("/user")
@RestController
public class UserCtrl extends BaseCtrl {
    @Autowired
    private UserService userService;


    @PostMapping("select/Id")
    public Result selectByOpenId(@RequestParam("openId") String openId) {
        /*如果该用户不存在就注册一个返回插入的操作结果*/
        if(userService.selectByOpenId(openId)==null)
        {
            Result res=new Result();
            Map<String,Object> mp=new HashedMap();
            mp.put("openId",openId);
            mp.put("createTime",new Date());
            mp.put("updateTime",new Date());
          if(userService.addWxUser(mp)==1)
          {
              res.setResult(userService.selectByOpenId(openId));
              res.setStatus(MessageConstVar.SuccessCode,MessageConstVar.Success);
              return res;
          }
          else
          {
              res.setStatus(MessageConstVar.ErrorCode,MessageConstVar.Error);
              return res;
          }

        }
        /*如果已经存在该用户就返回用户信息*/
        return send(userService.selectByOpenId(openId));
    }

    @PostMapping("modify/password")
    public Result modifyPassword(
            @RequestParam("phone") String phone,
            @RequestParam("newPassword") String newPassword,
            @RequestParam("oldPassword") String oldPassword) {
        /*封装好返回的结果*/
        Result res = new Result();
        try {
            // WxUser user = getUserFromSession(phone);//通过redis迅速读取用户信息
            WxUser user = userService.selectByphone(phone);
            if (user == null) {
                res.setStatus(MessageConstVar.NoLoginCode, MessageConstVar.NoLogin);
                res.setResult(0);
                return res;
            }
            String decrypt = StringUtil.decrypt(user.getPassword());//将读取到的用户信息的密码解密
            if (decrypt.equals(oldPassword)) //如果密码验证正确就可以修改密码
            {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("phone", phone);
                map.put("password", StringUtil.encrypt(newPassword));
                int count = userService.updatePassword(map); //返回update操作之后影响的行数
                if (count != 0)  //受影响的话重新拉取用户的数据写入缓存 由于暂时没有按手机号查询这个接口这边先注释掉直接返回成功
                {
                    // WxUser user1 = userService.login(phone);
                    //  setUserToSession(phone,user1);
                    res.setStatus(MessageConstVar.SuccessCode, MessageConstVar.Success);
                    res.setResult(1);
                    return res;
                }
                res.setStatus(MessageConstVar.SystemcErrCode, MessageConstVar.SystemcErr);
                res.setResult(0);
                return res;
            }
            res.setStatus(MessageConstVar.PasswordErrCode, MessageConstVar.PasswordErr);
            res.setResult(0);
            return res;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            res.setStatus(MessageConstVar.ErrorCode, MessageConstVar.Error);
            res.setResult(0);
            return send(res);
        }
    }

        /**
     * @author lilanlan
     * @date 2019-08-07 20:09
     */
    @PostMapping("modify/Phone")
    public Result modifyPhone(
            @RequestParam("oldPhone") String oldPhone,
            @RequestParam("newPhone") String newPhone,
            @RequestParam("code") String code,
            @RequestParam("password") String password,
            HttpServletRequest request) {
        /*封装好返回的结果*/
        Result res1 = new Result();
        try {
            WxUser user = userService.selectByphone(oldPhone);
            if (user == null) {
                return send(MessageConstVar.NoLoginCode, MessageConstVar.NoLogin);
            }
            String code2 = this.getValueFromSession("code", request);
            if (code2.equals(code)) {
                String decrypt1 = StringUtil.decrypt(user.getPassword());//将读取到的用户信息的密码解密
                if (decrypt1.equals(password)) //如果密码验证正确就可以修改手机号
                {
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("newphone", newPhone);
                    map.put("oldphone", oldPhone);
                    Integer count = userService.updatePhone(map);
                    if (count != 0) {
                        res1.setStatus(MessageConstVar.SuccessCode, MessageConstVar.Success);
                        res1.setResult(1);
                        return res1;
                    }
                    return send(MessageConstVar.SystemcErrCode, MessageConstVar.SystemcErr);
                }
                return send(MessageConstVar.PasswordErrCode, MessageConstVar.PasswordErr);
            }
            return send(MessageConstVar.PhoneCode, MessageConstVar.PhoneCo);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return send(MessageConstVar.ErrorCode, MessageConstVar.Error);
        }
    }

}


